package com.hymjweb.demo.hymj.master_bill_manage.auto.mappper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.master_bill_manage.auto.bean.BMasterBillAutoPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface BMasterBillAutoMapper {


    class BMasterBillAutoMapperProvider {

        public String findAll(Map param) {
            String sql = new SQL() {{
                SELECT("t.* ");
                FROM("b_master_bill_auto t");
                WHERE(" 1=1 ");
            }}.toString();

            /**
             * keyMasterBillCode: ""
             * keyOpenBillEndDate: ""
             * keyOpenBillStartDate: ""
             * keyReceiveCustomerName: ""
             * keyTransportWayId: ""
             * keyFreightAmountStatusId: ""
             * keyAccountCheckStatusId : ""
             */

            if (param != null) {

                Long keyTransportWayId = DataTypeUtil.toLongObject(param.get("keyTransportWayId"));
                String keyMasterBillCode = DataTypeUtil.toStringObject(param.get("keyMasterBillCode"));
                String keyOpenBillEndDate = DataTypeUtil.toStringObject(param.get("keyOpenBillEndDate"));
                String keyOpenBillStartDate = DataTypeUtil.toStringObject(param.get("keyOpenBillStartDate"));
                Long keyFreightAmountStatusId = DataTypeUtil.toLongObject(param.get("keyFreightAmountStatusId"));
                Long keyAccountCheckStatusId = DataTypeUtil.toLongObject(param.get("keyAccountCheckStatusId"));
                Long keyOutUnitId = DataTypeUtil.toLongObject(param.get("keyOutUnitId"));

                if (keyTransportWayId != null) {
                    sql += " and t.transport_way_id = " + keyTransportWayId;
                }

                if (keyMasterBillCode != null) {
                    sql += " and t.id like " + DataTypeUtil.quoteAllLikeStr(keyMasterBillCode);
                }

                if (keyOpenBillStartDate != null) {
                    if (keyOpenBillEndDate != null) {
                        int d = keyOpenBillStartDate.compareTo(keyOpenBillEndDate);
                        if (d > 0) {
                            String tempDate = keyOpenBillStartDate;
                            keyOpenBillStartDate = keyOpenBillEndDate;
                            keyOpenBillEndDate = tempDate;
                        }
                        sql += " and (date_format(t.create_time,'%Y-%m-%d') between " + DataTypeUtil.quoteStr(keyOpenBillStartDate) + " and " + DataTypeUtil.quoteStr(keyOpenBillEndDate) + ")";
                    } else {
                        sql += " and date_format(t.create_time,'%Y-%m-%d') = " + DataTypeUtil.quoteStr(keyOpenBillStartDate);
                    }
                } else {
                    if (keyOpenBillEndDate != null) {
                        sql += " and date_format(t.create_time,'%Y-%m-%d') = " + DataTypeUtil.quoteStr(keyOpenBillEndDate);
                    }
                }

                if (keyFreightAmountStatusId != null) {
                    sql += " and t.freight_amount_status_id = " + keyFreightAmountStatusId;
                }

                if (keyAccountCheckStatusId != null) {
                    sql += " and t.account_check_status_id = " + keyAccountCheckStatusId;
                }

                if (keyOutUnitId != null) {
                    sql += " and t.transport_unit_id = " + DataTypeUtil.quoteStr(keyOutUnitId);
                }

            }
            return sql;
        }

        public String getBMasterBillAutoPojoListByInId(String ids) {
            String sql = new SQL() {{
                SELECT("t.* ");
                FROM("b_master_bill_auto t");
                WHERE("t.id in (" + ids + ")");
            }}.toString();
            return sql;
        }


        public String getBMasterBillAutoPojoById(String id) {
            String sql = new SQL() {{
                SELECT("t.* ");
                FROM("b_master_bill_auto t where t.id = " + DataTypeUtil.quoteStr(id));
            }}.toString();
            return sql;
        }


        public String insert(BMasterBillAutoPojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("b_master_bill_auto");
                VALUES("id", "#{id}");
                VALUES("transport_way_id", "#{transportWayId}");
                VALUES("transport_unit_id", "#{transportUnitId}");
                VALUES("transport_unit_master_bill_code_ids", "#{transportUnitMasterBillCodeIds}");
                VALUES("start_city_id", "#{startCityId}");
                VALUES("dest_city_id", "#{destCityId}");
                VALUES("auto_line_road_name", "#{autoLineRoadName}");
                VALUES("send_way_id", "#{sendWayId}");

                VALUES("pick_up_point_id", "#{pickUpPointId}");
                VALUES("pick_up_point_name", "#{pickUpPointName}");
                VALUES("pick_up_point_phone", "#{pickUpPointPhone}");
                VALUES("pick_up_point_address", "#{pickUpPointAddress}");

                VALUES("license_plate_number", "#{licensePlateNumber}");
                VALUES("delivery_time", "#{deliveryTime}");
                VALUES("driver", "#{driver}");
                VALUES("driver_phone", "#{driverPhone}");

                VALUES("goods_type_id", "#{goodsTypeId}");
                VALUES("total_number", "#{totalNumber}");
                VALUES("actual_weight", "#{actualWeight}");
                VALUES("chargeable_weight", "#{chargeableWeight}");
                VALUES("actual_load_total_weight", "#{actualLoadTotalWeight}");
                VALUES("weight_rate", "#{weightRate}");
                VALUES("volume", "#{volume}");
                VALUES("volume_rate", "#{volumeRate}");

                VALUES("freight_amount", "#{freightAmount}");
                VALUES("other_charges", "#{otherCharges}");
                VALUES("other_charges_comment", "#{otherChargesComment}");
                VALUES("master_bill_payment_method_id", "#{masterBillPaymentMethodId}");
                VALUES("total_freight_amount", "#{totalFreightAmount}");
                VALUES("drop_amount", "#{dropAmount}");
                VALUES("settlement_amount", "#{settlementAmount}");

                VALUES("open_master_bill_date", "#{openMasterBillDate}");
                VALUES("open_master_bill_department_id", "#{openMasterBillDepartmentId}");
                VALUES("open_master_bill_user_id", "#{openMasterBillUserId}");
                VALUES("comment", "#{comment}");
                VALUES("freight_amount_status_id", "#{freightAmountStatusId}");
                VALUES("drop_status_id", "#{dropStatusId}");
                VALUES("account_check_status_id", "#{accountCheckStatusId}");
                VALUES("settlement_status_id", "#{settlementStatusId}");

                VALUES("load_native_bill_ids", "#{loadNativeBillIds}");

                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }


        public String update(BMasterBillAutoPojo pojo) {
            String sql = new SQL() {{
                UPDATE("b_master_bill_auto t");
                SET("t.transport_way_id = #{transportWayId}");
                SET("t.transport_unit_id = #{transportUnitId}");
                SET("t.transport_unit_master_bill_code_ids = #{transportUnitMasterBillCodeIds}");
                SET("t.start_city_id = #{startCityId}");
                SET("t.dest_city_id = #{destCityId}");
                SET("t.auto_line_road_name = #{autoLineRoadName}");
                SET("t.send_way_id = #{sendWayId}");

                SET("t.pick_up_point_id = #{pickUpPointId}");
                SET("t.pick_up_point_name = #{pickUpPointName}");
                SET("t.pick_up_point_phone = #{pickUpPointPhone}");
                SET("t.pick_up_point_address = #{pickUpPointAddress}");

                SET("t.license_plate_number = #{licensePlateNumber}");
                SET("t.delivery_time = #{deliveryTime}");
                SET("t.driver = #{driver}");
                SET("t.driver_phone = #{driverPhone}");

                SET("t.goods_type_id = #{goodsTypeId}");
                SET("t.total_number = #{totalNumber}");
                SET("t.actual_weight = #{actualWeight}");
                SET("t.chargeable_weight = #{chargeableWeight}");
                SET("t.actual_load_total_weight = #{actualLoadTotalWeight}");
                SET("t.weight_rate = #{weightRate}");
                SET("t.volume = #{volume}");
                SET("t.volume_rate = #{volumeRate}");

                SET("t.freight_amount = #{freightAmount}");
                SET("t.other_charges = #{otherCharges}");
                SET("t.other_charges_comment = #{otherChargesComment}");
                SET("t.master_bill_payment_method_id = #{masterBillPaymentMethodId}");
                SET("t.total_freight_amount = #{totalFreightAmount}");

                SET("t.open_master_bill_date = #{openMasterBillDate}");
                SET("t.open_master_bill_department_id = #{openMasterBillDepartmentId}");
                SET("t.open_master_bill_user_id = #{openMasterBillUserId}");
                SET("t.comment = #{comment}");

                SET("t.drop_amount = #{dropAmount}");
                SET("t.settlement_amount = #{settlementAmount}");
                SET("t.freight_amount_status_id = #{freightAmountStatusId}");
                SET("t.drop_status_id = #{dropStatusId}");
                SET("t.account_check_status_id = #{accountCheckStatusId}");
                SET("t.settlement_status_id = #{settlementStatusId}");

                SET("t.load_native_bill_ids = #{loadNativeBillIds}");
                SET("t.update_time = #{updateTime}");
                SET("t.update_user_id = #{updateUserId}");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;

        }


        public String updatePojo(BMasterBillAutoPojo bMasterBillAutoPojo) {
            String sql = new SQL() {{
                UPDATE("b_master_bill_auto t");
                SET("t.transport_way_id = #{transportWayId}");
                SET("t.transport_unit_id = #{transportUnitId}");
                SET("t.transport_unit_master_bill_code_ids = #{transportUnitMasterBillCodeIds}");
                SET("t.start_city_id = #{startCityId}");
                SET("t.dest_city_id = #{destCityId}");
                SET("t.auto_line_road_name = #{autoLineRoadName}");
                SET("t.send_way_id = #{sendWayId}");

                SET("t.pick_up_point_id = #{pickUpPointId}");
                SET("t.pick_up_point_name = #{pickUpPointName}");
                SET("t.pick_up_point_phone = #{pickUpPointPhone}");
                SET("t.pick_up_point_address = #{pickUpPointAddress}");

                SET("t.license_plate_number = #{licensePlateNumber}");
                SET("t.delivery_time = #{deliveryTime}");
                SET("t.driver = #{driver}");
                SET("t.driver_phone = #{driverPhone}");

                SET("t.goods_type_id = #{goodsTypeId}");
                SET("t.total_number = #{totalNumber}");
                SET("t.actual_weight = #{actualWeight}");
                SET("t.chargeable_weight = #{chargeableWeight}");
                SET("t.actual_load_total_weight = #{actualLoadTotalWeight}");
                SET("t.weight_rate = #{weightRate}");
                SET("t.volume = #{volume}");
                SET("t.volume_rate = #{volumeRate}");

                SET("t.freight_amount = #{freightAmount}");
                SET("t.other_charges = #{otherCharges}");
                SET("t.other_charges_comment = #{otherChargesComment}");
                SET("t.master_bill_payment_method_id = #{masterBillPaymentMethodId}");
                SET("t.total_freight_amount = #{totalFreightAmount}");

                SET("t.open_master_bill_date = #{openMasterBillDate}");
                SET("t.open_master_bill_department_id = #{openMasterBillDepartmentId}");
                SET("t.open_master_bill_user_id = #{openMasterBillUserId}");
                SET("t.comment = #{comment}");

                SET("t.drop_amount = #{dropAmount}");
                SET("t.settlement_amount = #{settlementAmount}");
                SET("t.freight_amount_status_id = #{freightAmountStatusId}");
                SET("t.drop_status_id = #{dropStatusId}");
                SET("t.account_check_status_id = #{accountCheckStatusId}");
                SET("t.settlement_status_id = #{settlementStatusId}");

                SET("t.load_native_bill_ids = #{loadNativeBillIds}");

                SET("t.update_time = #{updateTime}");
                SET("t.update_user_id = #{updateUserId}");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }


        public String updateStatusId(String statusFieldName, Long statusId, String billIds, Date updateTime, String updateUserId) {
            String sql = new SQL() {{
                UPDATE("b_master_bill_auto t");
                SET("t." + statusFieldName + " = " + statusId);
                SET("t.update_time = #{updateTime}");
                SET("t.update_user_id = #{updateUserId}");
                WHERE("t.id in (" + billIds + ")");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("b_master_bill_auto t");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

    }

    @Results(id = "bMasterBillAutoPojoMapId", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "transport_way_id", property = "transportWayId"),
            @Result(column = "transport_unit_id", property = "transportUnitId"),
            @Result(column = "transport_unit_master_bill_code_ids", property = "transportUnitMasterBillCodeIds"),
            @Result(column = "start_city_id", property = "startCityId"),
            @Result(column = "dest_city_id", property = "destCityId"),

            @Result(column = "auto_line_road_name", property = "autoLineRoadName"),
            @Result(column = "send_way_id", property = "sendWayId"),
            @Result(column = "pick_up_point_id", property = "pickUpPointId"),
            @Result(column = "pick_up_point_name", property = "pickUpPointName"),
            @Result(column = "pick_up_point_phone", property = "pickUpPointPhone"),
            @Result(column = "pick_up_point_address", property = "pickUpPointAddress"),

            @Result(column = "license_plate_number", property = "licensePlateNumber"),
            @Result(column = "delivery_time", property = "deliveryTime"),
            @Result(column = "driver", property = "driver"),
            @Result(column = "driver_phone", property = "driverPhone"),
            @Result(column = "goods_type_id", property = "goodsTypeId"),
            @Result(column = "total_number", property = "totalNumber"),

            @Result(column = "actual_weight", property = "actualWeight"),
            @Result(column = "chargeable_weight", property = "chargeableWeight"),
            @Result(column = "actual_load_total_weight", property = "actualLoadTotalWeight"),
            @Result(column = "weight_rate", property = "weightRate"),
            @Result(column = "volume", property = "volume"),
            @Result(column = "volume_rate", property = "volumeRate"),

            @Result(column = "freight_amount", property = "freightAmount"),
            @Result(column = "other_charges", property = "otherCharges"),
            @Result(column = "other_charges_comment", property = "otherChargesComment"),
            @Result(column = "master_bill_payment_method_id", property = "masterBillPaymentMethodId"),
            @Result(column = "drop_amount", property = "dropAmount"),
            @Result(column = "settlement_amount", property = "settlementAmount"),
            @Result(column = "total_freight_amount", property = "totalFreightAmount"),

            @Result(column = "open_master_bill_date", property = "openMasterBillDate"),
            @Result(column = "open_master_bill_department_id", property = "openMasterBillDepartmentId"),
            @Result(column = "open_master_bill_user_id", property = "openMasterBillUserId"),
            @Result(column = "comment", property = "comment"),

            @Result(column = "freight_amount_status_id", property = "freightAmountStatusId"),
            @Result(column = "drop_status_id", property = "dropStatusId"),
            @Result(column = "account_check_status_id", property = "accountCheckStatusId"),
            @Result(column = "settlement_status_id", property = "settlementStatusId"),

            @Result(column = "load_native_bill_ids", property = "loadNativeBillIds"),

            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId")
    })
    @SelectProvider(type = BMasterBillAutoMapperProvider.class, method = "findAll")
    List<BMasterBillAutoPojo> findAll(Map param);

    @ResultMap("bMasterBillAutoPojoMapId")
    @SelectProvider(type = BMasterBillAutoMapperProvider.class, method = "getBMasterBillAutoPojoById")
    BMasterBillAutoPojo getBMasterBillAutoPojoById(String id);

    @ResultMap("bMasterBillAutoPojoMapId")
    @SelectProvider(type = BMasterBillAutoMapperProvider.class, method = "getBMasterBillAutoPojoListByInId")
    List<BMasterBillAutoPojo> getBMasterBillAutoPojoListByInId(String ids);

    @InsertProvider(type = BMasterBillAutoMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BMasterBillAutoPojo pojo);

    @UpdateProvider(type = BMasterBillAutoMapperProvider.class, method = "update")
    int update(BMasterBillAutoPojo pojo);


    @UpdateProvider(type = BMasterBillAutoMapperProvider.class, method = "updateStatusId")
    int updateStatusId(String statusFieldName, Long statusId, String billIds, Date updateTime, String updateUserId);

    @DeleteProvider(type = BMasterBillAutoMapperProvider.class, method = "delete")
    int delete(String id);

}
